

cc.Class({
    extends: cc.Component,

    properties: {
        //摄像机的移动速度.
        moveSpeed:0,
        //摄像机与角色的偏移量.
        offsetToPlayer:0,
        //每隔一定层数提升难度.
        levelUpLayer:0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.isFollow = false;
        this.level = 0;
        this.shakeDuration = 0.05;
        this.active = false;
    },

    start () {

    },

    update (dt) {
        if(!this.isFollow) return;
        this.node.position = this.node.position.add(cc.v2(0,this.moveSpeed*dt));
    },

    //更新摄像机的位置.
    updateCameraPos(playerPosY, isShaking){
        this.isFollow = false;
        var waitTime = isShaking ? this.shakeDuration * 4 : 0;
        var cameraMove = cc.moveTo(0.5,cc.v2(0,playerPosY + this.offsetToPlayer));
        this.scheduleOnce(function() {
            // 这里的 this 指向 component
            this.node.runAction(cameraMove);
        }, waitTime);
        
        this.isFollow = true;
    },

    //检测层级是否需要提升摄像机移动速度.
    detectLevel(){
        
    },

    //摄像机左右震动.
    shake(){
        var shake1 = cc.moveBy(this.shakeDuration,cc.v2(0,-40));
        var shake2 = cc.moveBy(this.shakeDuration,cc.v2(0,50));
        var shake3 = cc.moveBy(this.shakeDuration,cc.v2(0,-14));
        var shake4 = cc.moveBy(this.shakeDuration,cc.v2(0,4));
        var shake = cc.sequence(shake1,shake2,shake3,shake4);
        this.node.runAction(shake);
    },

    doMove(){
        if(this.active) return;
        this.isFollow = true;
        this.active = true;
    }

});
